package com.hjwoo.app.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.GridView;

import com.hjwoo.app.R;

public class DiscoverFragment extends Fragment
{

    GridView descover_gv;
    private int[]    imageViewIds;
    private String[] menuStrs;
    private WebView  webView;

    @Override
    public void onResume()
    {
        super.onResume();
        getView().setFocusableInTouchMode(true);
        getView().requestFocus();
        getView().setOnKeyListener(new View.OnKeyListener()
        {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event)
            {

                if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_BACK)
                {
                    //这里处理返回事件
                    if (keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack())
                    {
                        webView.goBack();
                        return true;
                    }
                }
                return false;
            }
        });
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        View view = inflater.inflate(R.layout.fragment_discover, container, false);
//        initView(view);
        webView = (WebView) view.findViewById(R.id.found_webview);
        webView.setOnKeyListener(new View.OnKeyListener()
        {
            @Override
            public boolean onKey(View view, int i, KeyEvent keyEvent)
            {
//                if(keyEvent.getAction() == KeyEvent.ACTION_DOWN){
                if (i == KeyEvent.KEYCODE_BACK && webView.canGoBack())
                {
                    webView.goBack();
                    return true;
                }
//                }
                return false;
            }
        });
        webView.loadUrl("http://www.shiyuanshangmao.com/dist/find.html");
//        webView.loadUrl("file:///android_asset/find.html");
        WebSettings wSettings = webView.getSettings();
        wSettings.setBuiltInZoomControls(true);
        wSettings.setUseWideViewPort(true);
        wSettings.setLoadWithOverviewMode(true);
        wSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        wSettings.setJavaScriptEnabled(true);
        wSettings.setDomStorageEnabled(true);
        wSettings.setAllowFileAccess(true);
        wSettings.setAppCacheEnabled(true);
        wSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        wSettings.setCacheMode(WebSettings.LOAD_DEFAULT);

        webView.setWebViewClient(new WebViewClient()
        {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url)
            {
                //返回值是true的时候控制去WebView打开，为false调用系统浏览器或第三方浏览器
                view.loadUrl(url);
                return true;
            }
        });
        return view;
    }

//    void initView(View view)
//    {
//        descover_gv = (GridView) view.findViewById(R.id.descover_gv);
//
//        imageViewIds = new int[]{R.mipmap.discover_flow, R.mipmap.discover_list,
//                                 R.mipmap.discover_halp, R.mipmap.discover_commission, R.mipmap.discover_credit,
//                                 R.mipmap.discover_notarization, R.mipmap.discover_aboutus, R.mipmap.discover_commitment, R.mipmap.discover_more};
//
//        menuStrs = new String[]{"办理流程", "资料清单", "帮助中心", "查询建委", "查询征信点", "查公证处", "关于我们", "黄金承诺", "更多精彩",};
//
////        List<DiscoverMenuObject> myMenuLS = new ArrayList<DiscoverMenuObject>();
////        myMenuLS.add()
//
//        DiscoverGVAdapter leadsLVAdapter = new DiscoverGVAdapter(view.getContext(), imageViewIds, menuStrs);
//        descover_gv.setAdapter(leadsLVAdapter);
//    }

}